﻿package 
{
	import away3d.cameras.Camera3D;
	import away3d.containers.ObjectContainer3D;
	import away3d.containers.View3D;
	import away3d.core.render.Renderer;
	import away3d.core.utils.Cast;
	import away3d.loaders.Collada;
	import away3d.loaders.Loader3D;
	import away3d.loaders.data.AnimationData;
	import away3d.materials.BitmapFileMaterial;
	import away3d.webcamFaceDetector.src.WebcamFaceDetector;

	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.net.URLRequest;
	import flash.net.URLLoader;

	public class demo3 extends Sprite
	{
		//the material array
		//private var material:Array=[["model/clown.DAE","model/clown.JPEG"],["model/crown.dae","model/crown.jpg"]];
		private var material:Array=new Array("dae/clown.DAE","dae/crown.DAE","dae/eyeshade2 - 副本.DAE",
											 "dae/eyeshade.DAE","dae/gas mask.DAE","dae/Glass.DAE","dae/hat.DAE",
											 "dae/long hat.DAE","dae/mustache.DAE","dae/pig nose.DAE");
		private var select:int = 0;
		private var modelArray:Array;
		private var count:int;

		private var _view:View3D;
		private var _camController:HoverDragController;
		private var _dae:ObjectContainer3D;

		private var _container:ObjectContainer3D;

		//webcamface detector
		private var face_detect:WebcamFaceDetector;
		private var detectTimer:Timer;

		public function demo3()
		{
			select = 0;
			init();
		}

		private function init():void
		{
			//LoadXML();
			
			_view = new View3D({x:300,y:300});
			//_camController = new HoverDragController(_view.camera, stage);
			//_camController.radius = 500;
			
			trace(material[3]);
			_dae = Collada.load(material[3],{mouseEnabled:false,scaling:"60"});
			
			_dae.x = -200;
			_dae.y = 85;

			_container = new ObjectContainer3D;
			_container.addChild(_dae);

			_view.scene.addChild(_container);

			this.addChild(_view);

			//face detect
			face_detect = new WebcamFaceDetector;
			if ((face_detect == null))
			{
				trace("face null!!!");
			}
			this.addChildAt(face_detect,0);

			//update
			detectTimer = new Timer(50,0);
			detectTimer.addEventListener(TimerEvent.TIMER,detect_timer);
			detectTimer.start();
			
		}

		private function LoadXML()
		{
			var myXml:XML = new XML  ;
			var xml_url:String = "model.xml";
			var myXmlUrl:URLRequest = new URLRequest(xml_url);
			var myLoader:URLLoader = new URLLoader(myXmlUrl);
			myLoader.addEventListener("complete",xmlLoaded);
			//complete为这个注册侦听器的名称，第2个参数为侦听对象的函数名称。;
			//............
			function xmlLoaded(event:Event):void
			{
				myXml = new XML(myLoader.data);
				count = myXml. @ num;
				modelArray = [count];//数组长度为10.根据XML给出的数据定义保存请求序列的第一维长度
				for (var i:int = 0; i < count; ++i)
				{
					modelArray[i] = [4];//定义保存请求序列的第二维长度，之后才可填充数据
					modelArray[i][0] = myXml.model[i]. @ Name;
					modelArray[i][1] = myXml.model[i]. @ scaling;
					modelArray[i][2] = myXml.model[i]. @ x;
					modelArray[i][3] = myXml.model[i]. @ y;
				}
				trace(modelArray[0]);
			}
			//trace(modelArray[0]);
		}

		private function detect_timer(e:TimerEvent):void
		{
			//face detect
			if ((face_detect == null))
			{
				trace("no face detect!");
			}
			if (face_detect._rects != null && face_detect._rects.length > 0)
			{
				var faceRect = face_detect._rects[0] as Sprite;
				//_container.x=faceRect.x-faceRect.width;
				//_container.y=-faceRect.y+faceRect.height;
				_container.x = faceRect.x;
				_container.y =  -  faceRect.y;
				//trace(face_detect._rects.length);
				//trace("x: "+faceRect.x+" "+colladaContainer.x);
				//trace("y: "+faceRect.y+" "+colladaContainer.y);
				//trace("changed length: "+face_detect._rects.length);
			}

			//_container.rotationY += 1;
			//_container.rotationX += .1;
			_view.render();
		}
	}
}